package cn.itcast.netty.c3;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TestNettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1. 准备eventLoop对象
        EventLoop eventLoop = new NioEventLoopGroup().next();
        //2. 主动创建Promise,结果容器
        DefaultPromise<Object> promise = new DefaultPromise<>(eventLoop);

        new Thread(()->{
            // 任意一个线程执行计算
            log.debug("开始计算");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 向promise填充结果
            promise.setSuccess(80);
        }).start();
        log.debug("等待结果");
        //接收结果线程
        log.debug("结果：{}",promise.get());
    }
}
